package com.lucher.tangstore.controller;

import com.lucher.tangstore.dao.SupplierMapper;
import com.lucher.tangstore.po.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierController extends BaseController{
	@Autowired
	private SupplierMapper supplierMapper;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Supplier form) {
		return "supplier/findSupplierList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Supplier form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<>();
		
		Integer total = supplierMapper.findByCount(form);
		List<Supplier> list = supplierMapper.findByListForPage(form);
		
		for (Supplier supplier : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("id", supplier.getId());
			objectMap.put("name", supplier.getName());
			objectMap.put("shortname", supplier.getShortname());
			objectMap.put("address", supplier.getAddress());
			objectMap.put("sendaddress", supplier.getSendaddress());
			objectMap.put("contacter", supplier.getContacter());
			objectMap.put("mobile", supplier.getMobile());
			objectMap.put("remark", supplier.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Supplier form,Model model) {
		//查询所有的工序信息
		//List<Workorder> workorderlist = workorderDao.findByList(new Workorder());
		//model.addAttribute("workorderlist", workorderlist);
		return "supplier/addSupplier";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Supplier form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getName())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入供应商名称");
			return responseJson;
		} else {
			Supplier oldSupplier = supplierMapper.findByName(form.getName());
			if (oldSupplier != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此供应商名称已经存在");
				return responseJson;
			}
		}
		
		//保存供应商信息
		Integer supplierId = supplierMapper.insertSelective(form);

		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Supplier form,Model model) {
		Supplier supplier = supplierMapper.selectByPrimaryKey(form.getId());
		form.setSupplier(supplier);
		
		return "supplier/updateSupplier";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	@Transactional
	public Map<String,Object> update(Supplier form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Supplier supplier_old = supplierMapper.selectByPrimaryKey(form.getId());

		if(supplier_old == null){
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，该供应商不存在");
			return responseJson;
		}
		
		if ("".equals(form.getName())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，供应商名称不能为空");
			return responseJson;
		} else {
			Supplier oldSupplier = supplierMapper.findByName(form.getName());
			if (oldSupplier != null && !oldSupplier.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，供应商名称已经存在");
				return responseJson;
			}
		}
		
		// 修改信息
		int flag = supplierMapper.updateByPrimaryKeySelective(form);

		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Supplier form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除供应商
		supplierMapper.deleteByPrimaryKey(form.getId());

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Supplier> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Supplier supplier : formlist) {
			//删除材料
			supplierMapper.deleteByPrimaryKey(supplier.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
     * 获取下拉列表框Json值
     */
	@ResponseBody //此标签表示返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	@RequestMapping(value="/getSupplierCombogridJson")
	public List<Map<String,Object>> getSupplierCombogridJson(Supplier form) {  
	    List<Supplier> supplierList = supplierMapper.findByList(form);
	    List<Map<String, Object>> supplierCombogridJson = new ArrayList<Map<String, Object>>();
	    //封装属性结构信息
	    for(Supplier supplier:supplierList){  
	    	HashMap<String,Object> supplierMap = new HashMap<String, Object>();
			supplierMap.put("id", supplier.getId());
			supplierMap.put("name", supplier.getName());
			supplierMap.put("shortname", supplier.getShortname());
			supplierMap.put("address", supplier.getAddress());
			supplierMap.put("sendaddress", supplier.getSendaddress());
			supplierMap.put("contacter", supplier.getContacter());
			supplierMap.put("mobile", supplier.getMobile());
			supplierMap.put("remark", supplier.getRemark());
			supplierCombogridJson.add(supplierMap);
	    }  
	   return supplierCombogridJson;
	}  
	
}
